#include <stdio.h>

int mostFrequent(int *cipher, int n, int index)
{
	int freq[128]={0}, i=index, max = 0, k=0;
	while (i<n) {
		freq[cipher[i]]++;
		i +=3;
	}
	i = 0;
	while (i<128) {
		if (freq[i] > max) {
			max=freq[i];
			k = i;
		}
		i++;
	}
	return k;
}

void guessKey(int *cipher, int n, int attempt, char *key)
{
	int i;
	if (attempt == 1)
		for (i=0; i<3; i++) key[i] = mostFrequent(cipher, n, i)^' ';

	if (attempt == 2)
		for (i=0; i<3; i++) key[i] = mostFrequent(cipher, n, i)^'e';
	key[3]='\0';
}

int main()
{
	FILE *fp;
	fp = fopen("cipher1.txt", "r");
	int cipher[1201], i=0, j;
	while (!feof(fp)) fscanf(fp, "%d", &cipher[i++]);
	fclose(fp);
	char key[4];
	guessKey(cipher, 1201, 1, key);
	printf("%s\n", key);
}
